﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using MvcContrib.Pagination;

using ENail.Domain.Core;
using Equation.Enterprise.Framework.Domain.Model;
using Equation.Enterprise.Framework.Web.Model;
using Equation.Enterprise.Utilities.DesignByContract;
using ENail.Domain.Core.Contract.Services;


namespace ENail.Portal.Web.Models
{
    public class VendorViewModel: 
        BaseViewModel<Int16>
    {
        [Required(ErrorMessage="Vendor name is empty")]
        public string Name { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string Zip { get; set; }
        public string Country { get; set; }
        public string Phone { get; set; }
        public string Email { get; set; }
        public string Fax { get; set; }
    }

    public class VendorListViewItem : BaseViewModel<Int16>
    {
        public string Name { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string Zip { get; set; }
        public string Country { get; set; }
        public string Phone { get; set; }
        public string Email { get; set; }
        public string Fax { get; set; }
        public bool IsSelected { get; set; }
    }

    public class VendorViewModelMapper: ViewModelMapper<VendorViewModel, Vendor>
    {
        public override void MapDomainToView(Vendor domainModel, VendorViewModel viewModel)
        {
 	        Check.Require(domainModel != null);
            Check.Require(viewModel != null);

            viewModel.Id = domainModel.Id;
            viewModel.Name = domainModel.Name;
            viewModel.Address= domainModel.Address;
            viewModel.City = domainModel.City;
            viewModel.State = domainModel.State;
            viewModel.Zip = domainModel.Zip;
            viewModel.Country = domainModel.Country;
            viewModel.Phone = domainModel.Phone;
            viewModel.Email = domainModel.Email;
            viewModel.Fax = domainModel.Fax;
        }

        public override void MapViewToDomain(VendorViewModel viewModel, Vendor domainModel)
        {
            Check.Require(viewModel != null);
            Check.Require(domainModel != null);

            domainModel.Id = viewModel.Id;
            domainModel.Name = viewModel.Name;
            domainModel.Address = viewModel.Address;
            domainModel.City = viewModel.City;
            domainModel.State = viewModel.State;
            domainModel.Zip = viewModel.Zip;
            domainModel.Country = viewModel.Country;
            domainModel.Phone = viewModel.Phone;
            domainModel.Email = viewModel.Email;
            domainModel.Fax = viewModel.Fax;
        }
    }

    public class VendorListViewModelMapper : ListViewModelMapper<VendorListViewItem, Vendor>
    {
        public const int DEFAULT_PAGE = 1;
        public const int PAGE_SIZE = 3;

        public override void MapDomainToView(List<Vendor> domainModel, GenericListViewModel<VendorListViewItem> viewModel)
        {
            Check.Require(domainModel != null);
            Check.Require(viewModel != null);

            viewModel.List = (from e in domainModel
                              where
                                  e.IsActive &&
                                  (viewModel.Filter == null || viewModel.Filter.Trim() == "" ||
                                   e.Name.ToLower().Contains(viewModel.Filter.Trim().ToLower()))
                              select new VendorListViewItem
                                         {
                                             Id = e.Id,
                                             Name = e.Name,
                                             Address = e.Address,
                                             City = e.City,
                                             State = e.State,
                                             Zip=e.Zip,
                                             Country=e.Country,
                                             Phone=e.Phone,
                                             Email=e.Email,
                                             Fax=e.Fax,
                                  IsSelected = false
                              })
                              .OrderBy(x => x.Name)
                              .AsPagination(viewModel.Page ?? DEFAULT_PAGE, PAGE_SIZE);
        }

        public override void MapViewToDomain(GenericListViewModel<VendorListViewItem> viewModel, List<Vendor> domainModel)
        {
            throw new NotImplementedException();
        }
    }
}